import * as api from '@/api'

export default {
    namespaced:true,
    state:{
        products:[]
    },
    mutations:{
        SET_PRODUCT(state,payload){
            state.products = payload
        },
        //让库存减少
        DEL_INVENTORY(state,payload){
                console.log(payload)
                let target = state.products.find(item=>item.id==payload.id)
                if(target){
                    target.inventory --
                }
        },
        //让库存增加
        ADD_INVENTORY(state,payload){
            let target = state.products.find(item=>item.id==payload.id)
                if(target){
                    target.inventory ++
                }
        }
    },
    actions:{
       async FETCH_PRODUCT({commit}){
            let res = await api.fetchProduct()
            console.log(res)
            commit('SET_PRODUCT',res.data)
        }
    },
    getters:{}
}